<template>
  <div class="user-layout-register">
    <div class="content">
      <a-icon type="left" style="cursor: pointer" @click="$parent.isForget = false" />
      <span class="title">{{ $t('all.autotext389') }}</span>
    </div>
    <a-form ref="formRegister" :form="form" id="formRegister">
      <a-form-item>
        <a-input
          size="large"
          :maxLength="11"
          :placeholder="$t('all.autotext396')"
          v-decorator="[
            'phone',
            {
              rules: [
                {
                  required: true,
                  message: $t('user.phone-number.required'),
                  pattern: /^(1)\d{10}$/
                },
                { validator: this.handlePhoneCheck }
              ],
              validateTrigger: ['change', 'blur']
            }
          ]"
        >
          <a-select slot="addonBefore" size="large" defaultValue="+86">
            <a-select-option value="+86">+86</a-select-option>
            <a-select-option value="+87">+87</a-select-option>
          </a-select>
        </a-input>
      </a-form-item>

      <a-row :gutter="9">
        <a-col class="gutter-row" :span="15">
          <a-form-item>
            <a-input
              size="large"
              autocomplete="off"
              type="text"
              :placeholder="$t('user.login.mobile.verification-code.placeholder')"
              v-decorator="['code', { rules: [{ required: true, message: $t('all.autotext639') }], validateTrigger: 'blur' }]"
            >
              <a-icon slot="prefix" type="mail" :style="{ color: '#D6DFEC' }" />
            </a-input>
          </a-form-item>
        </a-col>
        <a-col class="gutter-row" :span="9">
          <a-button
            ghost
            type="primary"
            class="getCaptcha"
            :disabled="state.smsSendBtn"
            @click.stop.prevent="getCaptcha"
            v-text="(!state.smsSendBtn && $t('user.register.get-verification-code')) || state.time + ' s'"
          ></a-button>
        </a-col>
      </a-row>

      <a-popover
        placement="rightTop"
        :trigger="['focus']"
        :getPopupContainer="(trigger) => trigger.parentElement"
        v-model="state.passwordLevelChecked"
      >
        <template slot="content">
          <div :style="{ width: '240px' }">
            <div :class="['user-register', passwordLevelClass]">{{ $t(passwordLevelName) }}</div>
            <a-progress :percent="state.percent" :showInfo="false" :strokeColor="passwordLevelColor" />
            <div style="margin-top: 10px">
              <span>{{ $t('all.autotext221') }}</span>
            </div>
          </div>
        </template>
        <a-form-item>
          <a-input-password
            autocomplete="new-password"
            size="large"
            @click="handlePasswordInputClick"
            :placeholder="$t('all.autotext342')"
            :maxLength="30"
            v-decorator="[
              'pwd',
              {
                rules: [{ required: true, message: $t('all.autotext734') }, { pattern: /^\S*(?=\S{8,})(?=\S*\d)(?=\S*[A-Z])(?=\S*[a-z])(?=\S*[!@#$%^&*.? ])\S*$/, message: $t('all.autotext342') }, { validator: this.handlePasswordLevel }],
                validateTrigger: ['change', 'blur']
              }
            ]"
          ></a-input-password>
        </a-form-item>
      </a-popover>

      <a-form-item>
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="login-button"
          :loading="registerBtn"
          @click.stop.prevent="handleSubmit"
          :disabled="registerBtn"
          block
        >
          {{ $t('all.autotext1276') }}
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script>
import { getSendCode, getReset } from '@/api/login'
import { scorePassword } from '@/utils/util'
import { deviceMixin } from '@/store/device-mixin'

const levelNames = {
  0: 'user.password.strength.short',
  1: 'user.password.strength.low',
  2: 'user.password.strength.medium',
  3: 'user.password.strength.strong'
}
const levelClass = {
  0: 'error',
  1: 'error',
  2: 'warning',
  3: 'success'
}
const levelColor = {
  0: '#ff0000',
  1: '#ff0000',
  2: '#ff7e05',
  3: '#52c41a'
}
export default {
  name: '',
  components: {},
  mixins: [deviceMixin],
  data () {
    return {
      form: this.$form.createForm(this),
      code: 333333,
      state: {
        time: 60,
        level: 0,
        smsSendBtn: false,
        passwordLevel: 0,
        passwordLevelChecked: false,
        percent: 10,
        progressColor: '#FF0000'
      },
      registerBtn: false,
      timer: null
    }
  },
  computed: {
    passwordLevelClass () {
      return levelClass[this.state.passwordLevel]
    },
    passwordLevelName () {
      return levelNames[this.state.passwordLevel]
    },
    passwordLevelColor () {
      return levelColor[this.state.passwordLevel]
    }
  },
  methods: {
    handlePasswordLevel (rule, value, callback) {
      if (value === '' || value === undefined || value === null) {
        return callback()
      }
      console.log('scorePassword ; ', scorePassword(value))
      if (value.length >= 6) {
        if (scorePassword(value) >= 30) {
          this.state.level = 1
        }
        if (scorePassword(value) >= 60) {
          this.state.level = 2
        }
        if (scorePassword(value) >= 80) {
          this.state.level = 3
        }
      } else {
        this.state.level = 0
        callback(new Error(this.$t('user.password.strength.msg')))
      }
      console.log(123)
      this.state.passwordLevel = this.state.level
      this.state.percent = this.state.level * 33

      callback()
    },

    handlePhoneCheck (rule, value, callback) {
      callback()
    },

    getCaptcha (e) {
      e.preventDefault()
      const {
        form: { validateFields },
        state,
        $notification
      } = this

      validateFields(['phone'], { force: true }, (err, values) => {
        if (!err) {
          state.smsSendBtn = true

          getSendCode({ phone: values.phone })
            .then((res) => {
              $notification.success({
                message: this.$t('all.autotext730'),
                description: this.$t('all.autotext527'),
                duration: 8
              })
              this.timer = setInterval(() => {
                if (state.time-- <= 0) {
                  state.time = 60
                  state.smsSendBtn = false
                  window.clearInterval(this.timer)
                }
              }, 1000)
            })
            .catch((err) => {
              state.time = 60
              state.smsSendBtn = false
              this.getVerification(err)
            })
        }
      })
    },

    handlePasswordInputClick () {
      if (!this.isMobile) {
        this.state.passwordLevelChecked = true
        return
      }
      this.state.passwordLevelChecked = false
    },

    handleSubmit (e) {
      this.state.passwordLevelChecked = false
      console.log(e)
      e.preventDefault()
      const {
        form: { validateFields },
        state
      } = this

      const validateFieldsKey = ['phone', 'code', 'pwd']
      validateFields(validateFieldsKey, { force: true }, (err, values) => {
        if (!err) {
          const loginParams = {}
          loginParams.phone = values.phone
          loginParams.code = values.code
          loginParams.pwd = values.pwd
          state.registerBtn = true
          getReset(loginParams)
            .then((res) => this.loginSuccess(res))
            .catch((err) => this.requestFailed(err))
            .finally(() => {
              state.registerBtn = false
            })
          // this.$message.error('请将信息填写完整')
        }
      })
    },
    loginSuccess (res) {
      this.$message.success(this.$t('all.autotext397'), 10)
      // this.$router.push({ name: 'login' })
      this.$parent.isForget = false
    },
    requestFailed () {
      this.$message.error(this.$t('all.autotext918'), 10)
    },
    getVerification () {
      this.$message.error(this.$t('all.autotext933'), 10)
    }
  },
  watch: {
    'state.passwordLevel' (val) {
      console.log(val)
    }
  },
  destroyed () {
    if (this.timer) {
      clearInterval(this.timer)
    }
  }
}
</script>
<style lang="less">
.user-register {
  &.error {
    color: #ff0000;
  }

  &.warning {
    color: #ff7e05;
  }

  &.success {
    color: #52c41a;
  }
}

.user-layout-register {
  .ant-input-group-addon:first-child {
    background-color: #fff;
  }
}
</style>
<style lang="less" scoped>
.login-button {
  margin: auto;
  background-color: #3D92FF;
  border: none;
}
.content {
  display: flex;
  align-items: center;
  font-size: 24px;
  font-weight: 600;
  color: #181818;
  margin-bottom: 30px;
  .title {
    font-size: 23px;
    font-weight: bold;
    margin-left: 12px;
  }
}
.user-layout-register {
  ::v-deep .ant-input-suffix {
    color: #ABB8CE;
  }
  & > h3 {
    font-size: 16px;
    margin-bottom: 20px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .register-button {
    width: 50%;
  }

  .login {
    float: right;
    line-height: 40px;
  }
}
</style>
